package cn.jbolt.common.service;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import org.eclipse.jetty.websocket.server.WebSocketHandler.Simple;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.StringUtils;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import cn.jbolt.common.model.Wlxx;

public class QrService {
	private static int WIDTH = 9000;
	private static int HEIGHT = 9000;
	private static String FORMAT = "png";

	public String[] createDataMatrix(String wlh, int num, int zdsj, int zxsj) {
		Date date = new Date();
		String[] codeArr = new String[num];
		Wlxx wlxx = Wlxx.dao.findById(wlh);
		for (int i = 0; i < num; i++) {

			// 定义二维码参数
			Map hints = new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置编码
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置容错等级
			hints.put(EncodeHintType.MARGIN, 5); // 设置默认边距2
			Random random = new Random();
			int randNum = random.nextInt(zxsj - zdsj + 1) + zxsj;
			date.setTime(date.getTime() + randNum * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
			try {
				String code = wlxx.get("wlms2") + "/" + sdf.format(date);
				codeArr[i] = sdf.format(date);

				BitMatrix bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.DATA_MATRIX, 900000, 900000,
						hints);
				Path path = new File("C:/download/barpdf/tmpImg1/qrTmp" + sdf.format(date) + ".png").toPath();
				try {
					MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path); // 写到指定目录下
					BufferedInputStream in = new BufferedInputStream(
							new FileInputStream("C:/download/barpdf/tmpImg1/qrTmp" + sdf.format(date) + ".png")); // 读取图片
					Image bi = ImageIO.read(in); // 字节流转图片对象
					// 获取图像的高度宽度
					int height = bi.getHeight(null);
					int width = bi.getWidth(null);
					// 构建图片流
					BufferedImage tag = new BufferedImage(width * 3, height * 3, BufferedImage.TYPE_INT_RGB);
					// 绘制改变尺寸后的图
					tag.getGraphics().drawImage(bi, 0, 0, width * 3, height * 3, null);
					// 输出流
					BufferedOutputStream out = new BufferedOutputStream(
							new FileOutputStream("C:/download/barpdf/tmpImg/" + codeArr[i] + ".png"));
					// JPEGImageEncoder encoder =
					// JPEGCodec.createJPEGEncoder(out);
					// encoder.encode(tag);
					ImageIO.write(tag, "PNG", out);
					in.close();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (WriterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return codeArr;
	}

	public String[] createbarcode() {

		return null;

	}
}
